package leetcode.lt100;

//Definition for singly-linked list.
class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }

    @Override
    public String toString() {
        return next == null ? "" + val : "" + val + next.toString();
    }

    public ListNode append(int i) {
        this.next = new ListNode(i);
        return next;
    }
}

class Solution_61 {
    public ListNode rotateRight(ListNode head, int k) {
        if (head == null)
            return head;
        ListNode start = head;
        ListNode end = head;
        int l = 1;
        while (end.next != null) {
            l++;
            end = end.next;
        }

        end.next = start;

        k = l - k % l;

        for (int i = 1; i < k; i++) {
            start = start.next;
        }

        head = start.next;

        start.next = null;

        return head;
    }
}